﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetComponentsDx.Enums;
using System.Reflection;
using System.Collections;
using ProgNetComponents.Classes;
using System.ComponentModel;

namespace ProgNetComponentsDx.Browsers.Design
{
    public class BrowserTypeConverter : ProgNetComponents.Design.TypeConverters.CollectionItemTypeConverterAdv<Browser>
    {
        /*
        protected override System.Reflection.ConstructorInfo GetConstructor(Browser item)
        {
            if (item.IsStatic)
            {
                return typeof(Browser).GetConstructor(new Type[]{
                typeof(string));
                typeof(string));
                typeof(string));
                typeof(string));
                typeof(BrowserDisplayType));
                typeof(BrowserConfigDisplayType));
                typeof(string));
                typeof(string));
                typeof(BrowserColumn[]));
                typeof(bool)
            });
            }
            else if (item.DisplayType != BrowserDisplayType.ConfigSpecified &&
                item.DefaultConfigDisplayType.ToString() == item.DisplayType.ToString() &&
                item.Group.IsNull() &&
                item.Filter.IsNull())
            {
                return typeof(Browser).GetConstructor(new Type[]{
                typeof(string));
                typeof(string));
                typeof(string[]));
                typeof(string));
                typeof(string));
                typeof(BrowserDisplayType));
                typeof(BrowserColumn[])
                });
            }
            else
                return typeof(Browser).GetConstructor(new Type[]{
                typeof(string));
                typeof(string));
                typeof(string[]));
                typeof(string));
                typeof(string));
                typeof(BrowserDisplayType));
                typeof(BrowserConfigDisplayType));
                typeof(string));
                typeof(string));
                typeof(BrowserColumn[])
            });
        }
        */
        protected override TypedObjectCollection GetConstructorParamters(Browser item)
        {
            TypedObjectCollection a = new TypedObjectCollection();
            if (item.IsStatic)
            {
                a.AddRange(item.Name, item.Caption, item.ValueMember, item.DisplayMember, item.DisplayType);
                a.AddRange(item.DefaultConfigDisplayType, item.Group, item.Filter, item.Columns.ToArray(), item.RefreshOnPopup);
            }
            else if (item.DisplayType != BrowserDisplayType.ConfigSpecified &&
                item.DefaultConfigDisplayType.ToString() == item.DisplayType.ToString() &&
                item.Group.IsNull() &&
                item.Filter.IsNull() &&
                item.CustomColumnQuery.IsNull() &&
                item.CustomFilterQuery.IsNull() &&
                item.CustomFromQuery.IsNull())
            {
                a.AddRange(item.Name, item.Caption, item.Tables, item.ValueMember, item.DisplayMember, item.DisplayType, item.Columns.ToArray());
            }
            else
            {
                a.AddRange(item.Name, item.Caption, item.Tables, item.ValueMember, item.DisplayMember, item.DisplayType, item.DefaultConfigDisplayType, item.Group, item.Filter, item.Columns.ToArray());
                a.AddRange(item.CustomColumnQuery, item.CustomFromQuery, item.CustomFilterQuery);
            }
            return a;
        }
    }

    public class BrowserGroupTypeConverter : ProgNetComponents.Design.TypeConverters.CollectionItemTypeConverter<BrowserGroup>
    {
        protected override System.Reflection.ConstructorInfo GetConstructor(BrowserGroup item)
        {
            ConstructorInfo ci = null;
            if (item.Items.Count == 0)
            {
                if (item.Groups.Count == 0)
                    ci = item.GetType().GetConstructor(new Type[] { typeof(string) });
                else
                    ci = item.GetType().GetConstructor(new Type[] { typeof(string), typeof(BrowserGroup[])});
            }
            else
            {
                if (item.Groups.Count == 0)
                    ci = item.GetType().GetConstructor(new Type[] { typeof(string), typeof(Browser[]) });
                else
                    ci = item.GetType().GetConstructor(new Type[] { typeof(string), typeof(BrowserGroup[]), typeof(Browser[]) });
            }
            return ci;
        }

        protected override object[] GetConstructorParamters(BrowserGroup item)
        {
            if (item.Items.Count == 0)
            {
                if (item.Groups.Count == 0)
                    return new object[] { item.Name };
                else
                    return new object[] { item.Name, item.Groups.ToArray() };
            }
            else
            {
                if (item.Groups.Count == 0)
                    return new object[] { item.Name, item.Items.ToArray() };
                else
                    return new object[] { item.Name, item.Groups.ToArray(), item.Items.ToArray() };
            }
        }
    }
}
